import java.io.*;
import java.net.*;
import java.util.Vector;

public class ServerListener implements Runnable
{
	private Vector<Thread> clientProcessors;
	public ServerListener()
	{
		clientProcessors=new Vector<Thread>();
	}
	@Override
	public void run(){
		ServerSocket serverSocket = null;
		Players.Init();
		try {
			serverSocket = new ServerSocket(8008);
			serverSocket.setSoTimeout(30);
			while(!Thread.currentThread().isInterrupted()){
				try{
					Socket clientSocket = serverSocket.accept();
					if(Thread.currentThread().isInterrupted()) throw new InterruptedException();
					if(clientSocket!=null)
						{
						Thread clientProcessor = new Thread(new SocketListener(clientSocket));
						clientProcessors.add(clientProcessor);
						Players.RegisterPlayer(clientSocket);
						clientProcessor.start();
						}
				}
				catch (IOException e) {}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (InterruptedException e)
		{}
		finally
		{
			try {
				serverSocket.close();
				for(Thread clientProcessor:clientProcessors)
				{
					clientProcessor.interrupt();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
